package com.kly.cashmall.utils.function;

import android.content.Context;
import android.view.Gravity;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.hjq.toast.ToastUtils;
import com.kly.cashmall.utils.data.TextUtil;

import java.util.ArrayList;
import java.util.List;


public class ToastUtil {


  public static final int SHORT_DURATION_TIMEOUT = 2000; // 短吐司显示的时长
  public static final int LONG_DURATION_TIMEOUT = 3500; // 长吐司显示的时长


  public static List<String> ignoreList = new ArrayList<>();

  static {
    ignoreList.add("SUCCESS");
    ignoreList.add("success");
    ignoreList.add("ERROR");
    ignoreList.add("error");
  }

  private static boolean ignore(String str) {
    if (TextUtil.isEmpty(str)) {
      return true;
    }
    return ignoreList.contains(str);
  }

  public static void addIgnore(String ignoreText) {
    if (!ignoreList.contains(ignoreText)) {
      ignoreList.add(ignoreText);
    }
  }

  @Deprecated public static void showToast(Context context, final String toastText) {
    showToast(context, toastText);
  }

  public static void showToast(final String toastText) {
    showToast(toastText, Toast.LENGTH_SHORT);
  }

  @Deprecated public static void showToast(@Nullable final Context context, final String toastText,
      final int time) {
    showToast(toastText, time);
  }

  public static void showToast(final String toastText, final int time) {
    if (ignore(toastText)) {
      return;
    }
    ToastUtils.getToast().setDuration(time);
    TextView textView = ToastUtils.getView().findViewById(android.R.id.message);
    if(textView != null){
      textView.setGravity(Gravity.CENTER_HORIZONTAL);
    }
    ToastUtils.show(toastText);
  }

  /**
   * 不会随activity销毁而消失的toast
   */
  public static void showAppToast(String toastText) {
    showAppToast(toastText, Toast.LENGTH_SHORT);
  }

  public static void showAppToast(final String toastText, final int time) {
    if (ignore(toastText)) {
      return;
    }
    //ToastUtils.cancel();
    ToastUtils.getToast().setDuration(time);
    ToastUtils.show(toastText);
  }

  public static void cancelToast() {
    ToastUtils.cancel();
  }
}
